using System;
using System.Collections;
using System.ComponentModel;
using System.IO;
using EnvDTE;
using VSLangProj;
using Microsoft.Practices.RecipeFramework;
using Microsoft.Practices.ComponentModel;
using Microsoft.Practices.RecipeFramework.Library;
using VsWebSite;
using VSSDKAssist.Assets.Actions.Base;

namespace VSSDKAssist.Assets.Actions.VisualStudio
{
	/// <summary>
	/// Action that adds a assembly reference to a Project
	/// </summary>
	public class AddAssemblyReferenceAction : ActionBase
	{
		#region Properties

		private Project referringProject;

		/// <summary>
		/// Gets or sets the referring project.
		/// </summary>
		/// <value>The referring project.</value>
		[Input(Required = true)]
		public Project ReferringProject
		{
			get { return referringProject; }
			set { referringProject = value; }
		}

		string assemblyFilePath;

		/// <summary>
		/// Gets or sets the assembly file path.
		/// </summary>
		/// <value>The assembly file path.</value>
		[Input(Required = true)]
		public string AssemblyFilePath
		{
			get { return assemblyFilePath; }
			set { assemblyFilePath = value; }
		}
		#endregion

		/// <summary>
		/// Called when [execute].
		/// </summary>
		protected override void OnExecute()
		{
			if(DteHelper.IsWebProject(referringProject))
			{
				VSWebSite webProject = referringProject.Object as VSWebSite;

				if(webProject != null)
				{
					if(File.Exists(this.assemblyFilePath))
					{
						webProject.References.AddFromFile(this.assemblyFilePath);
					}
					else
					{
						webProject.References.AddFromGAC(this.assemblyFilePath);
					}
				}
			}
			else
			{
				VSProject vsProject = referringProject.Object as VSProject;
				if(vsProject != null)
				{
					vsProject.References.Add(this.assemblyFilePath);
				}
			}
		}

		/// <summary>
		/// Called when [undo].
		/// </summary>
		protected override void OnUndo()
		{
			//Do Nothing
		}
	}
}

